package com.anywhere.freightman.system.controller;

import com.anywhere.freightman.car.model.CarModel;
import com.anywhere.freightman.car.service.ICarService;
import com.anywhere.freightman.client.CarLocationTestOpenClient;
import com.anywhere.freightman.common.ContextKeys;
import com.anywhere.freightman.common.annotation.Permission;
import com.anywhere.freightman.common.entity.ReturnResp;
import com.anywhere.freightman.common.entity.car.LocationEntity;
import com.anywhere.freightman.common.entity.user.Account;
import com.anywhere.freightman.order.model.WaybillModel;
import com.anywhere.freightman.order.service.IWaybillService;
import com.anywhere.freightman.system.controller.resp.HomeOutlineResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("admin/home")
@Api(tags = "首页")
public class HomeController {

    @Autowired
    private ICarService carService;
    @Autowired
    private CarLocationTestOpenClient carLocationTestOpenClient;
    @Autowired
    private IWaybillService waybillService;

    @GetMapping("car/location")
    @ApiOperation("所有车辆定位")
    @Permission(code = "COMMON")
    public ReturnResp getAllCarLocation(HttpServletRequest httpRequest) throws Exception {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        List<CarModel> list = carService.getListByCompanyId(account.getCompanyId());
        List<String> carNumbers = list.stream().map(CarModel::getCarNumber).collect(Collectors.toList());
        List<LocationEntity> points = carLocationTestOpenClient.getLastLocation(carNumbers);
        return ReturnResp.buildOk(points);
    }

    @GetMapping("outline")
    @ApiOperation("简要统计")
    @Permission(code = "COMMON")
    public ReturnResp getOutline(HttpServletRequest httpRequest) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        List<CarModel> carList = carService.getListByCompanyId(account.getCompanyId());
        HomeOutlineResp resp = new HomeOutlineResp();

        long carCount = carList.size();
        long workingOrderCount = 0;
        long pubOrderCount = 0;
        long doneOrderCount = 0;
        long currentMonthCount = 0;


        List<WaybillModel> orderList = waybillService.getListByCompanyId(account.getCompanyId());
        for (WaybillModel item : orderList) {
            switch (item.getStatus()) {
                case WAIT_PICK:
                    pubOrderCount++;
                    break;
                case PICKED:
                case IN_TRANSIT:
                case ARRIVALS:
                case WAIT_PAY:
                    workingOrderCount++;
                    break;
                default:
                    break;
            }

            Date createdTime = item.getCreatedTime();
            Date now = new Date();
            if (createdTime.getYear() == now.getYear() && createdTime.getMonth() == now.getMonth()) {
                currentMonthCount++;
            }
        }
        resp.setCarCount(carCount);
        resp.setPubOrderCount(pubOrderCount);
        resp.setDoneOrderCount(doneOrderCount);
        resp.setWorkingOrderCount(workingOrderCount);
        resp.setCurrentMonthCount(currentMonthCount);
        return ReturnResp.buildOk(resp);
    }

    @GetMapping("report")
    @ApiOperation("获取订单统计数据")
    @Permission(code = "COMMON")
    public ReturnResp getReportList(HttpServletRequest httpRequest) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        Date endTime = new Date();
        Date startTime = DateUtils.addYears(endTime, -1);
        List<Map> reportList = waybillService.getReportList("%Y-%m", account.getCompanyId(), startTime, endTime);
        Map map = new HashMap<>();
        for (Map item : reportList) {
            map.put(item.get("time"), item.get("count"));
        }
        return ReturnResp.buildOk(map);
    }
}
